//后端 分类
import { Categories } from '@/entity/Categories';
import { Service, Inject } from 'typedi'
import { Entity } from '@/utils/orm'
import moment from 'moment';
import { JsonResult, msgList, res } from '@/utils/jsonResult';
import { Essayreq ,Essayupdata} from '@/m_req/Essayreq'
import { Token } from '@/m_req/Token'
import { Like } from 'typeorm';


@Service()
export class classifyDao {

    @Inject()
    orm!: Entity

    @Inject()
    json!: JsonResult<any>;

    

    //添加分类
    async classifyadd(params:{categories_title:string}): Promise<res<null>> {

        let ret = 
        await this.orm.source()
            .insert()
            .into(Categories)
            .values([
                {
                    categories_title:params.categories_title
                },
            ])
            .execute()

        return this.json.success(null, msgList.add)
    }


    //分类删除
    async classifydelete(params:{categories_title:string}): Promise<res<null>> {

        let fff = await this.orm.get(Categories).remove(<Categories>params)
        return this.json.success(null, msgList.DeleteSuccess)
    }


    

    //分类修改
    async classifyupdata(params:{categories_title:string}): Promise<res<null>> {

        let fff = await this.orm.get(Categories).save(<Categories>params)
        return this.json.success(null, msgList.Essayupdata)
    }
    
    //分类查询
    async classifyselect(params:{page:number}): Promise<res<Categories>> {
        console.log("params.page",params)
        let ss=Math.round((await this.orm.repository(Categories).getMany()).length/2)
        console.log("ss",ss)
        let dd= await this.orm.repository(Categories).skip((params.page*2)).take((2)).getMany()
        console.log("class",dd)
        
        return this.json.success(dd, msgList.Essayupdata,700,ss)
    }

    

    async classifyqueries(params:{text:string}): Promise<res<Categories>> {
        console.log("text",params.text)
       let ret= await this.orm.get(Categories).findBy({categories_title:Like(`%${params.text}%`),})
       console.log("ret",ret)
        return this.json.success(ret, msgList.Success)
    }
}